/**
 * 延迟信息处理类，
 * 目前暂定义 ：每天凌晨00:15 开始执行，仅处理到前天(含DateUtil.add(-2))
 **/
package net.tycmc.cty.ctyms.timejob.iems.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.ConfigurationException;

import com.tycmc.functions.util.MapGetter;

import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.cty.ctyms.timejob.common.DateRegUtil;
import net.tycmc.cty.ctyms.timejob.common.DynamicReadConfig;
import net.tycmc.cty.ctyms.timejob.common.ListUtil;
import net.tycmc.cty.ctyms.timejob.iems.dao.DelayDataParamStatDAO;

public class DelayDataParamStatService {
	private ListUtil listUtil = new ListUtil();
	private DelayDataParamStatDAO deDaStaDAO;
	public DelayDataParamStatDAO getDeDaStaDAO() {
		return deDaStaDAO;
	}
	public void setDeDaStaDAO(DelayDataParamStatDAO deDaStaDAO) {
		this.deDaStaDAO = deDaStaDAO;
	}
	public void startStat() {
		Date date1 = new Date();
		System.out.println("开始设备延迟数据参数统计:"+date1.toString());
		//获取此次处理的标志
		List<Map<String,Object>> listID = this.deDaStaDAO.getDefIDOfWorkMsgBfID();
		//必须能够获取上次处理的标志，否则此次也不进行处理
		if(null!=listID&&!listID.isEmpty()){
//			this.deDaStaDAO.updateFlagOfDMDR();
			List<Map<String,Object>> listOfVclAndMsg = this.deDaStaDAO.queryDelayMsgOfVclTime(listID);
			//设备延迟数据参数统计
			this.statDelayMsg(listOfVclAndMsg);
			
		}else{
			System.out.println("查询上次设备延迟数据参数统计处理到的ID出错，此次也不处理。");
		}
		System.out.println("此次设备延迟数据参数统计 完成，用时"+(new Date().getTime()-date1.getTime())/1000+"秒");
	}
	/**
	 * 设备延迟数据参数统计
	 * 
	 **/
	private void statDelayMsg(List<Map<String, Object>> srcList) {
		if(null!=srcList&&!srcList.isEmpty()){
			//按照Map<"1",大一设备序列><"2",小一设备序列> 进行组织数据
			Map<String,List<Map<String,Object>>> codeMap = this.listUtil.groupBy("vcltype", srcList);
			this.statVclOfSoftDelay(codeMap.get("1"),1);
			this.statSoVclDelay(codeMap.get("2"));
		}else{
			System.out.println("无需要处理的延迟信息");
		}
	}
	/**
	 * 小一设备延迟信息处理
	 * (根据软件版本号将小一设备分成403、412、414及其以上 三类，调用延迟信息处理)
	 **/
	private void statSoVclDelay(List<Map<String, Object>> soList) {
		if(null!=soList&&!soList.isEmpty()){
			//小一设备按照软件版本号，
			Map<String,List<Map<String,Object>>> softMap = this.listUtil.groupBy("Tmnl_SoftEdition", soList);
			List<Map<String,Object>> vcl403 = new ArrayList<Map<String,Object>>();
			List<Map<String,Object>> vcl412 = new ArrayList<Map<String,Object>>();
			List<Map<String,Object>> vcl414 = new ArrayList<Map<String,Object>>();
			String soft403 = "";
			String soft412 = "";
			String soft414 = "";
			try {
				soft403 = DynamicReadConfig.getConfigProperty("softveragreement.properties", "Vcl_J").toLowerCase();
				soft412 = DynamicReadConfig.getConfigProperty("softveragreement.properties", "Vcl_PP").toLowerCase();
				soft414 = DynamicReadConfig.getConfigProperty("softveragreement.properties", "Vcl_JPP").toLowerCase();
			} catch (ConfigurationException e) {
				
			}
			for(Iterator<String> it = softMap.keySet().iterator();it.hasNext();){
				String oneSoft = it.next();
				if(soft403.contains(oneSoft)){
					vcl403.addAll(softMap.get(oneSoft));
				}else if(soft412.contains(oneSoft)){
					vcl412.addAll(softMap.get(oneSoft));
				}else if(soft414.contains(oneSoft)){
					vcl414.addAll(softMap.get(oneSoft));
				}
			}
			this.statVclOfSoftDelay(vcl403,2);
			this.statVclOfSoftDelay(vcl412,3);
			this.statVclOfSoftDelay(vcl414,4);
		}
	}
	/**
	 * 设备延迟信息处理
	 */
	private void statVclOfSoftDelay(List<Map<String, Object>> vclList,int type) {
		if(null!=vclList&&!vclList.isEmpty()){
			//<key,value> = <设备ID,<该设备的延迟信息序列>>
			Map<String,List<Map<String,Object>>> vclMap = this.listUtil.groupBy("VehicleID", vclList);
			for(Iterator<String> it = vclMap.keySet().iterator();it.hasNext();){
				List<Map<String,Object>> oneVclTimes = vclMap.get(it.next());
				//按时间正序排序，
				this.listUtil.sortList(oneVclTimes, "MsgTime", true, false);
				for(Map<String,Object> oneTime:oneVclTimes){
					//校验最后一条是否是延迟信息（油耗、里程、工作小时数据 至少有一个不为0）
					if(this.deDaStaDAO.checkDelayIsLast(oneTime,type)){
						this.updateDelayParam(oneTime,type);
						break;
					}else{
						continue;
					}
				}
			}
		}
	}
	/**
	 * 重新计算设备延迟信息
	 * @param int type  1:大一；2:403；3:412；4：jpp设备（414及其以上）
	 **/
	private void updateDelayParam(Map<String, Object> vclTimeInfo, int type) {
		List<String> timeList = DateRegUtil.iterateTime(MapGetter.getString(vclTimeInfo, "MsgTime"),DateUtil.addDay(-2));
		if(null!=timeList&&!timeList.isEmpty()){
			String VehicleID = MapGetter.getString(vclTimeInfo, "VehicleID");
			for(String oneTime:timeList){
				switch(type){
				case 1:
					this.deDaStaDAO.updateBoDelayParam(oneTime,VehicleID);
					break;
				case 2:
					this.deDaStaDAO.updateCanDelayParam(oneTime,VehicleID);
					break;
				case 3:
					//2014-12-08 3.6.6.2 去掉PP参数统计 fujichuan
					//this.deDaStaDAO.updatePPDelayParam(oneTime,VehicleID);
					break;
				case 4:
					this.deDaStaDAO.updateJPPDelayParam(oneTime,VehicleID);
					break;
				default:
					break;
				}
			}
		}
	}
}
